import { getToken, setToken, removeToken, settimeout } from '../../utils/auth'
import { loginapi, getInfoapi, getUserDetailById } from '@/api/user'

const state = {
  token: getToken(),
  userinfo: {}
}
const mutations = {
  settoken(state, token) {
    state.token = token
    setToken(token)

  },
  removetoken(state) {
    state.token = null
    removeToken()
    // state.commit('removeuserinfo')
  },
  setuserinfo(state, userinfo) {
    state.userinfo = userinfo
  },
  removeuserinfo(state) {
    state.userinfo = {}
  }
}
const actions = {
  async login(context, data) {
    let res = await loginapi(data)
    context.commit('settoken', res)
    settimeout()
  },
  async getInfoapi(context) {
    let res = await getInfoapi()
    let res2 = await getUserDetailById(res.userId)
    const usersinfo = { ...res, ...res2 }
    context.commit('setuserinfo', usersinfo)
    console.log(usersinfo);
    return usersinfo
  },
  remoto(context) {
    context.commit('removetoken')
    context.commit('removeuserinfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}